#!/usr/bin/env bash


# ----------------------------------------------------------------------
# Filename	:  logFile
# Version	:  1.0
# Date		:  2022/02/28
# Author	:  Lz
# Email		:  liuzuo@kylinos.com.cn 
# History	:     
#            	   Version 1.0, 2022/02/10
# Function	:  日志文件权限
# Out		:        
#              0 => TPASS
#              1 => TFAIL
#              other=> TCONF
# ----------------------------------------------------------------------


# 测试主题
Title_Env_LTFLIB="安全加固(审计类) - 日志文件权限"


## TODO : 个性化,初始化
#   Out : 0=>TPASS
#         1=>TFAIL
#         2=>TCONF
TestInit_LTFLIB(){
	# 判断是否存在变量
	if [ "Z${LogFile_Audit_SSRFlag}" == "Z" ];then
		OutputRet_LTFLIB "${ERROR}"	
		TestRetParse_LTFLIB "未定义变量 LogFile_Audit_SSRFlag"
	fi

	# 所有文件
	Docs_ImportDoc=('/var/log/messages')
	for loop in `seq 0 ${docs_loop}`
	do
		tmpfile=${Docs_ImportDoc[${loop}]}

		# 判断文件是否存在
		if [ ! -f "${tmpfile}" ];then
			unset Docs_ImportDoc[${loop}]
			TConf_LLE "不存在文件 ${tmpfile}"
			continue
		fi
	done

        return $TPASS
}


## TODO : 清理函数
#   Out : 0=>TPASS
#         1=>TFAIL
#         2=>TCONF
TestClean_LTFLIB(){
        return $TPASS
}


## TODO : 测试用例集
#   Out : 0=>TPASS
#         1=>TFAIL
#         2=>TCONF
Testsuite_LTFLIB(){
        testcase_1

        return $TPASS
}


## TODO : 文件内容测试
#
testcase_1(){
	if [ "${LogFile_Audit_SSRFlag}" == "False" ];then
                OutputRet_LTFLIB "${TPASS}"
                TestRetParse_LTFLIB "当前无需对日志文件做权限限制"

		return ${TPASS}
	fi
	
	local tmpfile=""
	local power=""
	local power_num=""
	for tmpfile in ${Docs_ImportDoc[@]}
	do
		if [ ! -f "${tmpfile}" ];then
			continue
		fi

		# 获取User权限
		power=$(ls ${tmpfile} -al | awk -F '' '{print $2$3$4}')
		PowerParse "$power"
		power_num=$?
		# 判断User权限不超过６
		echo "${tmpfile} 文件User权限: $power(${power_num})"
		[ $power_num -le 6 ]
		CommRetParse_LTFLIB "要求${tmpfile} User权限不超过6" "False"

		# 获取Group权限
		power=$(ls ${tmpfile} -al | awk -F '' '{print $5$6$7}')
		PowerParse "$power"
		power_num=$?
		# 判断Group权限不超过4
		echo "${tmpfile} 文件Group权限: $power(${power_num})"
		[ $power_num -le 4 ]
		CommRetParse_LTFLIB "要求${tmpfile} Group权限不超过4" "False"

		# 获取other权限
		power=$(ls ${tmpfile} -al | awk -F '' '{print $8$9$10}')
		PowerParse "$power"
		power_num=$?
		# 判断other权限不超过4
		echo "${tmpfile} 文件Other权限: $power(${power_num})"
		[ $power_num -le 4 ]
		CommRetParse_LTFLIB "要求${tmpfile} Other权限不超过4" "False"
	done
}


## TODO : 权限计算
#   In  : 0=>文件权限位，例如rwx
#
PowerParse(){
	local power_num=0
	local power_letter="$1"
	local power_1=$(echo ${power_letter} | awk -F '' '{print $1}')
	local power_2=$(echo ${power_letter} | awk -F '' '{print $2}')
	local power_3=$(echo ${power_letter} | awk -F '' '{print $3}')
	
	case ${power_1} in
		r)
			let power_num=power_num+4
			;;
		-)
			true
			;;
		*)
			OutputRet_LTFLIB "$ERROR"
			TestRetParse_LTFLIB "第一权限位错误,应为:{r|-},当前为：${power_1}"
			;;
	esac
		
	case ${power_2} in
		w)
			let power_num=power_num+2
			;;
		-)
			true
			;;
		*)
			OutputRet_LTFLIB "$ERROR"
			TestRetParse_LTFLIB "第二权限位错误,应为:{w|-},当前为：${power_2}"
			;;
	esac

	case ${power_3} in
		x)
			let power_num=power_num+1
			;;
		-)
			true
			;;
		*)
			OutputRet_LTFLIB "$ERROR"
			TestRetParse_LTFLIB "第三权限位错误,应为:{x|-},当前为：${power_3}"
			;;
	esac

	return ${power_num}	
}


#----------------------------------------------#

source "${LIB_LTFLIB}"
Main_LTFLIB $@
